<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <link rel="stylesheet" href="./libs/layui/css/modules/layer/default/layer.css">
    <link rel="stylesheet" href="./libs/layui/css/layui.css">
    <script src="./libs/jquery/jquery.min.js"></script>
    <script src="./libs/layui/lay/modules/layer.js"></script>
    <script src="./js/http.js"></script>
    <script src="./libs/art-template/template-web.js"></script>
    <script src="./libs/layui/layui.js"></script>
    <script id="art_cagetory" type="text/html">
        {{each data}}
         <option value="{{$value.id}}">{{$value.name}}</option>
        {{/each}}
    </script>

    <script id="art_list" type="text/html">
        {{each data}}
        <tr>
            <td>{{$value.title}}</td>
            <td>{{$value.author}}</td>
            <td>{{$value.category}}</td>
            <td class="text-center">{{$value.date}}</td>
            <td class="text-center">{{$value.state}}</td>
            <td class="text-center">
                <a href="article_edit.html?id={{$value.id}}" class="btn btn-default btn-xs">编辑</a>
                <button class="btn btn-danger btn-xs delete">删除</a>
            </td>
        </tr>
        {{/each}}
    </script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <div class="form-inline">
                        <form id="search_form">
                            <input type="text" name="key" class="form-control input-sm" placeholder="输入文章标题...">
                            <select id="selCategory" name="type" class="form-control input-sm">
                                <option value="">所有分类</option>

                            </select>
                            <select id="selStatus" name="state" class="form-control input-sm">
                                <option value="">所有状态</option>
                                <option value="草稿">草稿</option>
                                <option value="已发布">已发布</option>
                            </select>
                            <input id="btnSearch" type="button" value="筛选" class="btn btn-success btn-sm">

                            <input type="reset" value="重置" class="btn btn-danger btn-sm">
                        </form>
                    </div>
                </div>
                <div class="col-xs-6">
                    <a href="./article_add.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div id="pager" class="row text-center">

            </div>
        </div>
    </div>

    <script>
        $(function () {
            reg_click_event_for_btnSearch();

            get_category_list();

            get_article_List();
        })

        // 1.0 给id="btnSearch"的搜索按钮注册click事件
        function reg_click_event_for_btnSearch() {
            $('#btnSearch').click(function () {
                get_article_List();
            })
        }

        // 2.0 给id="selCategory" 的select标签动态获取文章分类数据
        function get_category_list() {
            http('GET', '/admin/category/list', null, function (resData) {
                let selOptions = template('art_cagetory', resData);
                $('#selCategory').append(selOptions);
            });
        }

        // 3.0 定义get_article_List方法根据搜索条件获取文章数据列表
        function get_article_List(currentpage,pagesize) {
            currentpage = currentpage || 1;
            pagesize = pagesize || 6;
            
            let serach_params = $('#search_form').serialize();
            serach_params+='&page='+currentpage;
            serach_params+='&perpage='+pagesize;
            http('GET', '/admin/article/query', serach_params, function (resData) {
                let list = template('art_list', resData.data);
                $('.table tbody').html(list);

                // 设置分页控件
                initPager(currentpage,resData.data.totalCount);
            })
        }

        // 4.0 初始化分页控件
        function initPager(currentpage,totalCount) {
            currentpage = currentpage || 1;
            totalCount = totalCount || 0;
            
            layui.use('laypage', function () {
                var laypage = layui.laypage;

                //执行一个laypage实例
                laypage.render({                    
                    elem: 'pager' //注意，这里的 test1 是 ID，不用加 # 号
                    , count: totalCount, //数据总数，从服务端得到
                    limit: 6, //每页显示条数
                    curr: currentpage, //当前页码
                    // 点击分页数字触发
                    jump: function (obj, first) {
                        //obj包含了当前分页的所有参数，比如：
                        // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                        // console.log(obj.limit); //得到每页显示的条数
                        //first == true 是首次执行
                        // console.log(obj.curr,obj.limit)
                        if(!first){
                            get_article_List(obj.curr,obj.limit);
                        }                        
                    }
                });
            });
        }
    </script>
</body>

</html>